package com.doone.telmarket.action.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.doone.basefun.DocryptoSecret;
import com.doone.telmarket.action.admin.BaseActionSupport;
import com.doone.telmarket.action.admin.ServiceAction;
import com.doone.telmarket.domain.UserInfo;
import com.doone.telmarket.service.AdminLoginService;

/**
 * @package com.doone.telmarket.action.web;
 * @author 甘雨波
 * @description 登陆用ACTION
 * @date 2009-10-21
 */
public class LoginAction extends BaseActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String cityCode;

	private String accnbr;

	private String linkedNbr;

	private String loginType;

	private String timdeStr;

	private String checkStr;

	private String errorMsg;

	private String userName;

	private String passWord;

	private String checkWord;

	Logger log = Logger.getLogger(ServiceAction.class);

	@Autowired
	private AdminLoginService remoteAdminLoginService;

	@Action(value = "redirectPage", results = { @Result(name = "success", location = "/web/index.htm"),
			@Result(name = "error", location = "/web/error.jsp") })
	public String redirectPage() {

		try {
			UserInfo user = new UserInfo();
			user.setCityCode(cityCode);
			user.setAccnbr(accnbr);

			String result = identityVisa(cityCode, accnbr, loginType, timdeStr, checkStr);

			//log.info(result);

			if (result != "") {
				errorMsg = "加密串校验出错:" + result;
				return "error";
			} else {
				List<String> li = new ArrayList<String>();
				if (!("".equals(linkedNbr))) {
					String[] accnbr2 = linkedNbr.split(",");
					for (String element : accnbr2) {
						li.add(element);
					}
				}
				user.setLinkedAccnbr(li);
				user.setLoginType(loginType);
				session.put("userInfo", user);
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
			errorMsg = "登陆失败";
			return "error";
		}

	}

	@Action(value = "exit", results = { @Result(name = "success", location = "/web/index.htm"),
			@Result(name = "error", location = "/web/error.jsp") })
	public String exit() {

		try {
			session.put("userInfo", null);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			errorMsg = "操作失败";
			return "error";
		}

	}

	/**
	 * @package
	 * @author 甘雨波
	 * @description  验证加密
	 * @date 2009-10-21
	 * @param cityCode
	 * @param accnbr
	 * @param loginType
	 * @param dateStr
	 * @param MAC
	 * @return
	 */
	public String identityVisa(String cityCode, String accnbr, String loginType, String dateStr, String MAC) {
		String result = "";
		boolean stop = true;
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

		String privatekey = "aljOciAwemlDdfsaEdeorFdo";

		Date date;

		//log.info(cityCode+" "+accnbr+" "+loginType+" "+dateStr+" "+MAC);

		try {
			//验证时间戳
			date = df.parse(dateStr);
			long lTime = date.getTime();//请求时间
			long now = (new Date()).getTime(); //当前时间
			long diff = 0;
			if (now > lTime) {
				diff = now - lTime;
			} else {
				diff = lTime - now;
			}
			long min = diff / (60 * 1000); //转换成分钟
			if (min <= 10) {
				stop = false;
			} else {
				result = "请求时间戳超时";
			}

			//认证码验证
			if (!stop) {
				String vStr = cityCode + accnbr + loginType + dateStr;

				String str = DocryptoSecret
						.byte2hex(DocryptoSecret.encryptMode(privatekey.getBytes(), vStr.getBytes()));//3DES加密
				String DES = new String(str);
				String MD5Str = com.doone.basefun.MD5.getMD5ofString(DES);//MD5加密

				System.out.println("MD5Str:" + MD5Str);
				if (MD5Str.equals(MAC)) {
					stop = false;
				} else {
					result = "非法认证码(MAC)";
				}
			}
		} catch (java.text.ParseException e) {
			e.printStackTrace();
			result = "执行时发生异常";
		}
		return result;
	}

	@Action(value = "checkAdminLogin", results = { @Result(name = "success", location = "/admin/admin.jsp"),
			@Result(name = "error", location = "/admin/login.jsp") })
	public String adminLogin() {

		try {
			log.info(checkWord);
			log.info(session.get("verifyCode").toString());
			if (!checkWord.equals(session.get("verifyCode").toString())) {
				errorMsg = "验证码错误";
				return "error";
			} else {
				String pass = remoteAdminLoginService.getPassWord(userName);
				if(pass==null){
					errorMsg = "用户名不存在";
					return "error";
				}
				
				if ((!passWord.equals(pass))) {
					errorMsg = "密码验证出错";
					return "error";
				} else {
					session.put("isAdminLogined", "true");
					session.put("userName", userName);
					return "success";
				}
			}
		} catch (Exception e) {
			errorMsg = "系统繁忙,请你稍候再试";
			return "error";
		}
	}

	@Action(value = "loginOut", results = { @Result(name = "success", location = "/admin/login.jsp"),
			@Result(name = "error", location = "/admin/error.jsp") })
	public String loginOut() {
		try {
			session.put("isAdminLogined", "false");
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			errorMsg = "系统繁忙，请稍候再试";
			return "error";
		}
	}

	public void setLinkedNbr(String linkedNbr) {
		this.linkedNbr = linkedNbr;
	}

	public String getCityCode() {
		return cityCode;
	}

	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}

	public String getAccnbr() {
		return accnbr;
	}

	public void setAccnbr(String accnbr) {
		this.accnbr = accnbr;
	}

	public String getLinkedNbr() {
		return linkedNbr;
	}

	public String getTimdeStr() {
		return timdeStr;
	}

	public void setTimdeStr(String timdeStr) {
		this.timdeStr = timdeStr;
	}

	public String getCheckStr() {
		return checkStr;
	}

	public void setCheckStr(String checkStr) {
		this.checkStr = checkStr;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public String getLoginType() {
		return loginType;
	}

	public void setLoginType(String loginType) {
		this.loginType = loginType;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getCheckWord() {
		return checkWord;
	}

	public void setCheckWord(String checkWord) {
		this.checkWord = checkWord;
	}

}
